package com.wulinbus.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.fancy.common.utils.DateUtil;
import com.wulinbus.model.RemitOrderModel;
import com.wulinbus.model.ScriptModel;
import com.wulinbus.model.ScriptOwnModel;
import com.wulinbus.model.UserModel;
import com.wulinbus.service.RemitOrderService;
import com.wulinbus.service.ScriptService;
import com.wulinbus.service.TaskScriptService;
import com.wulinbus.service.UserService;

public class TaskScriptServiceImpl implements TaskScriptService {

	@Resource
	ScriptService scriptService;
	@Resource
	RemitOrderService remitOrderService;
	@Resource
	UserService userService;
	@Override
	public void execute() throws Throwable {
		
		
//		System.err.println(DateUtil.getSystemDateall() + "\r任务执行了....");
		List<ScriptModel> scriptList=scriptService.getCurrentTimeScriptList(DateUtil.getSystemDate());
		for(ScriptModel scriptModel:scriptList)
		{
			RemitOrderModel  remitOrderModel= remitOrderService.getRemitOrderModelBySid(scriptModel.getSid());
			if(remitOrderModel==null)
			{
				ScriptOwnModel scriptOwnModel=scriptService.getWinnerModel(scriptModel.getSid());
				if(scriptOwnModel!=null)
				{
					userService.addBalance(scriptOwnModel.getUid(), scriptModel.getCollect_budget());
					RemitOrderModel remitOrder=new RemitOrderModel();
					remitOrder.setSid(scriptModel.getSid());
					String s_user=userService.findCompByUid(scriptModel.getUid());
					remitOrder.setS_user(s_user);
					remitOrder.setOwn_sid(scriptOwnModel.getOwn_sid()); 
					remitOrder.setWinner_uid(scriptOwnModel.getUid());
					String w_user=userService.findCompByUid(scriptOwnModel.getUid());
					remitOrder.setW_user(w_user);
					remitOrder.setRemit_flag(1);
					remitOrder.setRvalue(scriptModel.getCollect_budget());
					remitOrderService.insertRemitOrder(remitOrder);
				}
			}
			
//			userService.updateBalanceOfNumOne(scriptModel.getCollect_budget(),scriptModel.getSid());
		}
	}

}
